package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.Md5Utils;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    // 注入rabbitTemplate，发送消息
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("list")
  //  @RequiresPermissions("用户管理")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<User> pageInfo = userService.findByPage(getLoginCompanyId(), pageNum, pageSize);
        System.out.println(pageInfo);
        request.setAttribute("pageInfo",pageInfo);
        return "/system/user/user-list";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        List<Dept> all = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",all);
        return "/system/user/user-add";
    }

    @RequestMapping("toUpdate")
    public String toUpdatee(String id){
        User user = userService.findById(id);
        List<Dept> all = deptService.findAll(getLoginCompanyId());
        request.setAttribute("user",user);
        request.setAttribute("deptList",all);
        return "/system/user/user-update";
    }

    @RequestMapping("edit")
    public String edit(User user){
        user.setCompanyId(getLoginCompanyId());
        user.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(user.getId())){
            userService.save(user);
            if (user.getEmail() != null && !"".equals(user.getEmail())) {
                String to = user.getEmail();
                // 处理发送邮件的业务
                String subject = "新员工入职通知";
                String content = "欢迎你来到SaasExport大家庭，我们是一个充满激情的团队，不是996哦！";
                // 发送消息
                Map<String,String> map = new HashMap<>();
                map.put("to",to);
                map.put("subject",subject);
                map.put("content",content);
                rabbitTemplate.convertAndSend("msg.email",map);
            }
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("delete")
    public String delete(String id){
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("roleList")
    public String roleList(String id){
        List<Role> roleList = roleService.findAll(getLoginCompanyId());
        request.setAttribute("roleList",roleList);
        User user = userService.findById(id);
        request.setAttribute("user",user);
        List<String> oldRole = userService.findOldRole(id);
        request.setAttribute("userRoleStr",oldRole);
        return "/system/user/user-role";
    }

    @RequestMapping("changeRole")
    public String changeRole(String userid,String[] roleIds){
        userService.cleanAllRoleByUserId(userid);
            if (roleIds.length > 0){
                for (int i = 0; i < roleIds.length; i++) {
                    userService.changeRole(userid,roleIds[i]);
                }
            }


        return "redirect:/system/user/list.do";
    }


}
